﻿using AutoMapper;
using MediatR;
using MyMES.Domain.yjb;
using MyMES.Domain.zmc;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Read.API.Application.Commends.Productionorderss;
using MyMES.Read.API.Dtos;

namespace MyMES.Read.API.Application.CommendHandlers.Productionorderss
{
    public class GetwoekderProessCommendHandlers : IRequestHandler<GetwoekderProessCommends, APIPaging<List<woekderProessProduprocDtos>>>
    {
        private readonly IBaseRepository<woekderProess> woekdersRepository;
        private readonly IBaseRepository<Process> processsRepository;
        private readonly IBaseRepository<Productionorders> productionRepository;
        private readonly IMapper mapper;

        public GetwoekderProessCommendHandlers(IBaseRepository<woekderProess> woekdersRepository, IBaseRepository<Process> processsRepository, IBaseRepository<Productionorders> productionRepository, IMapper mapper)
        {
            this.woekdersRepository = woekdersRepository;
            this.processsRepository = processsRepository;
            this.productionRepository = productionRepository;
            this.mapper = mapper;
        }




        /// <summary>
        /// 获取工序流程--中间表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIPaging<List<woekderProessProduprocDtos>>> Handle(GetwoekderProessCommends request, CancellationToken cancellationToken)
        {
            try
            {
                APIPaging<List<woekderProessProduprocDtos>> result = new APIPaging<List<woekderProessProduprocDtos>>();
                result.Code = APIEnum.查询成功;
                result.Msg = "查询成功";
                string sql = "select a.*,b.ProductionordersNo,c.ProcessNo,c.ProcessName,c.Processdescris FROM woekderproess a LEFT JOIN productionorders b on a.ProductionordersId=b.ProductionordersId LEFT JOIN process  c on a.ProcessID=c.ProcessID ";

                var woekder = woekdersRepository.SelectAsync(sql).Result;
                var Proces = processsRepository.SelectAsync(sql).Result;
                var Productiono = productionRepository.SelectAsync(sql).Result;

                var woekders = mapper.Map<List<woekderProessProduprocDtos>>(woekder);
                var Process = mapper.Map<List<woekderProessProduprocDtos>>(Proces);
                var Productionos = mapper.Map<List<woekderProessProduprocDtos>>(Productiono);


                foreach (var item in woekders)
                {
                    item.ProductionordersNo = Productionos.Where(x => x.ProductionordersId == item.ProductionordersId).FirstOrDefault()?.ProductionordersNo;
                    item.ProcessNo = Process.Where(x => x.ProcessID == item.ProcessID).FirstOrDefault()?.ProcessNo;
                    item.ProcessName = Process.Where(x => x.ProcessID == item.ProcessID).FirstOrDefault()?.ProcessName;
                    item.Processdescris = Process.Where(x => x.ProcessID == item.ProcessID).FirstOrDefault()?.Processdescris;

                }

                result.TotalCount = woekders.Count;
                result.TotalPage = (int)Math.Ceiling(result.TotalCount * 1.0 / request.PageSize);
                woekders = woekders.Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                result.data = woekders;
                return result;

            }
            catch (Exception e)
            {


                throw;
            }


        }
    }
}
